<template>
	<jk-dialog
		v-model="showDialog"
		title="选择-翻改机台"
		width="1200px"
		append-to-body
		@on-confirm="onConfirmBtn"
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
	>
		<div ref="box1" class="flex-end-center margin-bottom-10">
			<jk-select v-model="searchParams.workshopId" :option-list="workshopList" placeholder="区域" class="search-bar-item-size1 item-gap"></jk-select>
			<jk-select v-model="searchParams.processCode" :option-list="processList" value-key-name="code" placeholder="工序" size="small" class="search-bar-item-size1 item-gap" clearable />
			<jk-search-input v-model="searchParams.machineNameOrCode" placeholder="机台编号或名称" size="small" class="search-bar-item-size2 item-gap" clearable @on-enter="onSearchBtn" />
			<jk-search-button @click="onSearchBtn"></jk-search-button>
		</div>
		<jk-table
			ref="jkTable"
			:data="tableData"
			:loading="tableLoading"
			class="margin-top-10"
			:height="dialogTableHeight - 60"
			:checkbox-config="{checkMethod: checkboxMethod}"
			@checkbox-change="selectChangeEvent"
		>
			<vxe-column type="checkbox" width="60" align="center"></vxe-column>
			<vxe-table-column field="machineCode" title="机台" :width="90" />
			<vxe-table-column field="productOrderCode" title="生产类型" align="center" :width="80">
				<template slot-scope="scope">
					<span>{{ scope.row.ifTest ? '试产打样' : '正式生产' }}</span>
				</template>
			</vxe-table-column>
			<vxe-column title="客户" align="left" field="customerName" :min-width="140" />
			<vxe-table-column field="productName" title="品种" :min-width="140" />
			<vxe-table-column field="batchCode" title="批号" :min-width="140" />
			<vxe-table-column field="prdNoticeCode" title="通知单号" :width="140" />
			<vxe-table-column field="productOrderCode" title="生产订单" :width="140" />
			<vxe-table-column field="workshopName" title="区域" :width="90" />
		</jk-table>
		<footer ref="box2" class="flex-end-center margin-top-10">
			<jk-page
				location="dialog"
				:page-index="searchParams.pageNo"
				:page-sizes="searchParams.pageSizes"
				:page-size="searchParams.pageSize"
				:total="totalCount"
				@on-change="onChangePage"
			/>
		</footer>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-dialog-height';
    import { pageConfig } from '../../../../utils/jk/jk-tool';
    import { productionPrdNoticeMachineChangePage } from '../../../../api/production/xsRenovateOrder';

    export default {
        name: 'CheckEmpDialog',
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            checkKeyName: {
                type: String,
                default: 'id'
            },
            defaultGroupId: {
                type: Number,
                default: null
            },
            defaultSelectRows: {
                type: Array,
                default() {
                    return [];
                }
            },
            formData: {
                type: Object,
                default() {
                    return {};
                }
            }
        },
        mixins: [calHeight],
        data() {
            return {
                totalCount: 0,
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    machineNameOrCode: '',
                    workshopId: null,
                    processCode: '',
                    openingStates: [1, 2],
                    selectAll: true // 同时返回"正式生产"和"试产打样"的机台任务
                },
                processList: [],
                workshopList: [],
                tableLoading: false,
                tableData: [],
                showDialog: false,
                checkArr: []
            };
        },
        methods: {
            onChangePage(pageNo, pageSize) {
                this.$set(this.searchParams, 'pageNo', pageNo);
                this.$set(this.searchParams, 'pageSize', pageSize);
                this.getListRequest();
            },
            checkboxMethod(e) {
                return !e.row._disabled;
            },
            setCheckState(arr) {
                arr.forEach((x) => {
                    x._disabled = false;
                    this.defaultSelectRows.forEach(k => {
                        if (x.id === k[this.checkKeyName]) {
                            x._disabled = true;
                        }
                    });
                });
                this.tableData = arr;
                this.tableData.forEach((x, index) => {
                    if (x._disabled) {
                        this.$refs.jkTable.$refs.xTable.setCheckboxRow([this.tableData[index], this.tableData[index]], true);
                    }
                });
            },
            selectChangeEvent({ records }) {
                this.checkArr = records.filter(x => !x._disabled).map(x => {
                    return x;
                });
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onDeleteBtn(scope) {
                this.tableData.splice(scope.rowIndex, 1);
            },
            onConfirmBtn() {
                if (this.checkArr.length) {
                    const arr = this.checkArr.filter(x => !x._disabled);
                    this.$emit('on-confirm', arr);
                } else {
                    this.$tipsBar(this, 'custom', 'warning', '请选择数据');
                }
            },
            getListRequest() {
                this.tableLoading = true;
                return productionPrdNoticeMachineChangePage(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.totalCount = res.data.count;
                        this.setCheckState(res.data.res || []);
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            async getDependentData() {
                this.tableLoading = true;
                await this.getListRequest();
                this.tableLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.processList = this.$store.state.user.userProcess.processList;
                    this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                    this.$set(this.searchParams, 'workshopId', this.formData.workshopId);
                    this.$set(this.searchParams, 'processCode', this.formData.processCode);
                    this.getDependentData();
                }
            }
        }
    };
</script>
<style scoped>
</style>
